<?php
namespace app\index\controller;
use app\index\controller\Base;
use think\Db;

class Article extends Base
{
    public function index()
    {
        $id = input('article_id');
        $articles = Db::name('article')->find($id);
        $this->assign('articles',$articles);

        $cates = Db::name('cate')->find($articles['cateid']);
        $this->assign('cates',$cates);

        // 热度-点击数
        Db::name('article')->where(array('id'=>$id))->setInc('click');

        // 推荐
        $recres = Db::name('article')
            ->where(array('cateid'=>$cates['id'],'state'=>1))
            ->limit(8)
            ->select();
        $this->assign('recres',$recres);

        // 相关阅读
        $relevants = $this->relevant($articles['keywords'],$articles['id']);
        $this->assign('relevants',$relevants);

        // return THINK_VERSION;
        return $this->fetch();
    }

    public function relevant($keywords,$id){
        $arr = explode(',', $keywords);
        static $ralateres = array();
        foreach ($arr as $k=>$v) {
            $map['keywords'] = ['like','%'.$v.'%'];
            $map['id'] = ['neq',$id];
            $artres    = Db::name('article')
                ->where($map)
                ->order('id desc')
                ->limit(8)
                ->select();
            $ralateres = array_merge($ralateres,$artres);
        }
        if($ralateres){
            $ralateres=arr_unique($ralateres);
            return $ralateres;
        }

    }
}
